package test.utils;

import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.Data;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@Data
public class Util {
    public static EdgeDriver driver;

    public static EdgeDriver createDriver() {
        if(driver == null) {
            WebDriverManager.edgedriver().setup();
            EdgeOptions options = new EdgeOptions();
            //允许访问所有的链接
            options.addArguments("--remote-allow-origins=*");
            // 添加更多配置以解决连接问题
            options.addArguments("--no-sandbox");
            options.addArguments("--disable-dev-shm-usage");
            options.addArguments("--disable-gpu");
            options.addArguments("--ignore-certificate-errors");
            // 禁用日志
            System.setProperty("webdriver.edge.silentOutput", "true");
            options.setCapability("ms:loggingPrefs", new java.util.HashMap<String, String>() {{
                put("browser", "OFF");
                put("driver", "OFF");
            }});

            driver = new EdgeDriver(options);

            //隐式等待（全局情况，查找元素的时候都让它等待2秒）
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        }

        return driver;
    }

    public Util(String url) {
        driver = createDriver();
        driver.get(url);
    }
    public List<String> getTime(){
    //文件能不能按照天的维度按文件夹进行保存
    //文件格式 20230212-123030毫秒
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMdd");
        String filename = sim1.format(System.currentTimeMillis());
        String dirname = sim2.format(System.currentTimeMillis());
        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }
    /**
     * 获取屏幕截图，把所有的用例执行的结果保存下来
     */
    public void getScreenShot(String str) throws IOException, AWTException, InterruptedException {

        List<String> timeParts = getTime();

        // 构建路径
        String dirPath = Paths.get("./src/test/java/com/OJWebAutoTest", timeParts.get(0)).toString();
        String filename = String.format("%s_%s.png", str, timeParts.get(1));
        File destFile = new File(dirPath, filename);

        // 自动创建目录
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs(); // 创建多级目录
        }

        // 截图并保存
        // 使用 Robot 类截屏
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage screenFullImage = new Robot().createScreenCapture(screenRect);

        // 保存截图
        ImageIO.write(screenFullImage, "png", destFile);
        System.out.println("IDEA运行截图已保存至: " + destFile.getAbsolutePath());
    }

}